package com.dailyblue.java.project.oa.commons.service.impl;

import com.dailyblue.java.project.oa.commons.service.RedisService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void update(String key) {
        String[] array = key.split(",");
        String pr = array[0];
        String k = pr.substring(0, pr.indexOf(":")); // find
        String pid = array[1]; // 1
        String deptNo = array[2]; // 1
        // find::zhangsan,1,1
        ValueOperations value = stringRedisTemplate.opsForValue();
        // 会把null,pid,deptNo的数据删除
        value.set(k + "::null," + pid + "," + deptNo, "1", 1, TimeUnit.MILLISECONDS);
        value.set(k + "::null," + pid + ",null", "1", 1, TimeUnit.MILLISECONDS);
        value.set(k + "::null,null," + deptNo, "1", 1, TimeUnit.MILLISECONDS);
    }

    public void page(String key, int nowPage, int allPage) {
        ValueOperations value = stringRedisTemplate.opsForValue();
        for (int i = nowPage; i <= allPage; i++) {
            value.set(key + "::" + i, "1", 1, TimeUnit.MILLISECONDS);
        }
    }
}
